<template>
  <div class="fans">
    <scroller :on-refresh="refresh" :on-infinite="infinite">
      <div class="fans-list vtop" v-if="fansList.length"
       :class="{'loaded-data': fansList.length < 5, 'loadmore-data': fansList.length < 10}" >
        <div class="atten-item" v-for="(item, index) in fansList" :key="index">
          <div class="atteninfo" @click="$router.push({ path: `/follow/user/${item.member_id}` })">
            <img class="avatar" :src="item.avatar" alt="">
            <div class="atten-info">
              <div class="title">{{item.nickname}}</div>
              <div class="desc">关注 {{item.follow_num}} | 粉丝 {{item.fans_num}}</div>
              <div class="bouns">累计奖金<span class="money">{{item.bonus_total_price}}</span>元</div>
            </div>
          </div>
          <div class="atten-btn">
            <div class="btn" :class="{on: item.is_follow}" @click="handlefans(item)">{{item.is_follow?'已关注':'关注'}}</div>
          </div>
        </div>
      </div>
    </scroller>
    <div class="no-datas" v-if="!loading && !fansList.length">
      <img src="../../assets/imgs/icon-noattention@2x.png" alt="">
      <div class="desc">暂无粉丝</div>
    </div>
  </div>
</template>
<script>
import { mapActions } from 'vuex';

export default {
  data () {
    return {
      pageNum: 1,
      isPull: false, // 下拉
      loading: true,
      fansList: [
        // {
        //   member_id: '1',
        //   nickname: '嘻嘻啊1',
        //   avatar: 'https://www.try255.com/statics/avatar/1_160.png',
        //   mobile: '13067870772',
        //   follow_num: '1',
        //   fans_num: '1',
        //   bonus_total_price: '4.00'
        //  // is_follow: true
        // }
        // {
        //   member_id: '2',
        //   avatar: 'http://img0.imgtn.bdimg.com/it/u=1001413611,1569666981&fm=26&gp=0.jpg',
        //   nickname: '巴菲特',
        //   follow_num: 10,
        //   fans_num: 10,
        //   bonus_total_price: 100.00,
        //   is_follow: true
        // }, {
        //   member_id: '3',
        //   avatar: 'http://img2.touxiang.cn/file/20150513/c10267cfad438d8188172d0ccfe27d41.jpg',
        //   nickname: '烈阳在天',
        //   follow_num: 20,
        //   fans_num: 20,
        //   bonus_total_price: 200.00,
        //   is_follow: true
        // }, {
        //   member_id: '4',
        //   avatar: 'http://img2.touxiang.cn/file/20150513/c10267cfad438d8188172d0ccfe27d41.jpg',
        //   nickname: '没把握不下',
        //   follow_num: 30,
        //   fans_num: 30,
        //   bonus_total_price: 300.00,
        //   is_follow: true
        // }, {
        //   member_id: '5',
        //   avatar: 'http://img2.touxiang.cn/file/20150513/c10267cfad438d8188172d0ccfe27d41.jpg',
        //   nickname: '竞彩医疗兵',
        //   follow_num: 40,
        //   fans_num: 40,
        //   bonus_total_price: 400.00,
        //   is_follow: true
        // }
      ]
    }
  },
  methods: {
    ...mapActions({
      getFansList: 'attention/getFansList',
      getFollow: 'attention/getFollow',
      getUnFollow: 'attention/getUnFollow'
    }),
    refresh (done) {
      this.isPull = true;
      this.pageNum = 1;
      this.loading = true;
      this.getDatas(done);
    },
    infinite (done) {
      if (!this.isPull) {
        this.loading = true;
        this.getDatas(done);
      }
    },
    getDatas (done) {
      if (this.loading) {
        this.getFansList({ page: this.pageNum }).then(res => {
          this.loading = false;
          let listData = res.list || [];
          if (listData.length) {
            if (this.isPull) {
              this.isPull = false;
              this.fansList = [];
            }
            this.pageNum += 1;
            // for (let i = 0, j = 3; i < j; i++) {
            //   listData.push(
            //     {
            //       member_id: '1',
            //       nickname: '嘻嘻啊011',
            //       avatar: 'https://www.try255.com/statics/avatar/1_160.png',
            //       mobile: '13067870772',
            //       follow_num: '1',
            //       fans_num: '1',
            //       bonus_total_price: '4.00',
            //       is_follow: false
            //     }
            //   )
            // }
            const listDatas = this.fansList;
            this.fansList = listDatas.concat(listData);
            // this.fansList.push(...res.list);
            if (this.pageNum < res.page.total) {
              done(true);
            } else {
              done(2); ;
            }
          } else {
            done(2);
          }
        })
      }
    },
    handlefans (item) {
      console.log(item)
      // item.is_follow = !item.is_follow;
      if (item.is_follow) {
        this.getUnFollow({follower_id: item.member_id}).then(res => {
          item.is_follow = 0;
          console.log('res1', res, item);
        })
      } else {
        this.getFollow({follower_id: item.member_id}).then(res => {
          item.is_follow = 1;
          console.log('res2', res, item);
        })
      }
    }
  }
}
</script>
<style lang="less" scoped>
  @import '../../assets/styles/mixins.less';
  .fans {
    // background: #ffffff;
    .fans-list {
      margin-top: 0.12rem;
      background: red;
      padding-top: 0.4rem;
      .atten-item {
        display: flex;
        position: relative;
        background: #ffffff;
        &:last-child::after {
          display: none;
        }
        .border-1px-bottom(#d2d2d2);
        .atteninfo {
          display: flex;
          flex: 1;
          padding-top: 0.3rem;
        }
        .avatar {
          display: block;
          margin-left: 0.2rem;
          width: 1rem;
          height: 1rem;
          border-radius: 50%;
        }
        .atten-info {
          position: relative;
          flex: 1;
          margin-left: 0.2rem;
          padding-bottom: 0.2rem;
          // padding-right: 1.33rem;
          overflow: hidden;
          .title {
            font-size: 0.28rem;
            color: #2b2b2b;
            line-height: 0.36rem;
          }
          .desc {
            font-size: 0.24rem;
            color: #b9b9b9;
            line-height: 0.36rem;
          }
          .bouns {
            font-size: 0.24rem;
            color: #b9b9b9;
            line-height: 0.36rem;
            .money {
              color: #d44743;
            }
          }
        }
        .atten-btn {
          display: flex;
          // justify-content: flex-start;
          align-items: center;
          width: 1.33rem;
          .btn {
            // position: absolute;
            // right: 0.2rem;
            // top: 50%;
            // margin-top: -0.25rem;
            width: 1.13rem;
            height: 0.5rem;
            line-height: 0.5rem;
            font-size: 0.24rem;
            color: #ffffff;
            text-align: center;
            background-color: #d44743;
            border: 0.02rem solid #d44743;
            border-radius: 0.06rem;
            box-sizing: border-box;
            &.on {
              color: #d44743;
              background-color: #fff;
            }
          }
        }
        &:last-child {
          .atten-info::after {
            display: none;
          }
        }
      }
    }
    .no-datas {
      position: fixed;
      width: 100%;
      top: 1.92rem;
      bottom: 0;
      height: 100%;
      text-align: center;
      background: #ffffff;
      img {
        display: block;
        width: 4.15rem;
        height: 3.22rem;
        margin: 0 auto;
        padding-top: 0.8rem;
      }
      .desc {
        margin-top: 0.3rem;
        font-size: 0.22rem;
        color: #919191;
      }
    }
  }
</style>
